package com.sysmanage.common.tools.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
@Scope("prototype")
@Controller("tool.FileUploadControl")
public class FileUpload extends ActionSupport {
	private File[] upload;// 实际上传文件
	private String[] uploadContentType; // 文件的内容类型
	private String[] uploadFileName; // 上传文件名	
	private String prePath;
	
	public void upload()
	{
		String filePath = null;
		try
		{
			String targetDirectory = ServletActionContext.getServletContext()
					.getRealPath("/userUpload");// 获得路径
			if(upload.length>0)
			{
				String fileName = uploadFileName[0];// 上传的文件名
				if(prePath==null)
					prePath = "unkown";
				String realName =prePath + "_" + (new Date()).getTime() + fileName.substring(fileName.lastIndexOf("."));// 保存的文件名称

				File target = new File(targetDirectory, realName);
				FileUtils.copyFile(upload[0], target);// 上传至服务器的目录
				filePath = ServletActionContext.getServletContext().getContextPath()+"/userUpload/"+realName;
			}
			//ServletActionContext.getResponse().getOutputStream().print(filePath);
			outJsonPlainString("{\"success\":true,\"url\":\""+filePath+"\"}");
		} catch (Exception e)
		{
			e.printStackTrace();
			outJsonPlainString("{\"success\":false,\"url\":\""+filePath+"\"}");
		}
	}	
	
	
	
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}



	public String getPrePath() {
		return prePath;
	}



	public void setPrePath(String prePath) {
		this.prePath = prePath;
	}

	/** 
	  * @Title: outJsonPlainString 
	  * @Description: ContentType为text/plain 
	  * @param @param response 输出
	  * @param @param json json格式字符串
	  * @return void
	  * @throws 
	  */
	private void outJsonPlainString(String json){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/plain;charset=UTF-8");
		try {
			outString(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}                                                               
	/** 
	  * @Title: outString 
	  * @Description: 输出字符串
	  * @param response 返回
	  * @param str 传回的字符串
	  * @return void
	  * @throws 
	  */
	private void outString(HttpServletResponse response, String str) {
		try {
			PrintWriter out = response.getWriter();
			// out.write(str);
			out.println(str);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
